package com.jian.java;

import com.google.gson.Gson;
import com.jian.dao.impl.Student;
import com.jian.dao.impl.Teacher;
import com.jian.pojo.Result;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jiange
 * @create 2021-04-10-22:17
 */
public class LoginConnect extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletInputStream inputStream = req.getInputStream();

        resp.setContentType("text/plain;charset=utf-8");
        Map map = new HashMap();

        byte[] bytes = inputStream.readAllBytes();
        String data = new String(bytes, "utf-8");
        String[] split = data.split(",");
        boolean remeber = split[4].endsWith("true}");

        Gson gson = new Gson();
        Result login  = gson.fromJson(data, Result.class);
        String type = (String) login.getUser().get("type");
        String account = (String)login.getUser().get("account");
        String username = login.getUsername();
        String password = login.getPassword();



        if("student".equalsIgnoreCase(type)){
            if(remeber){
                Student student = new Student();
                com.jian.pojo.Student student1 = new com.jian.pojo.Student(username, password, account);
                int b = student.queryLogin(student1);
                System.out.println(b+"-----");
                if(b == 1){
                    map.put("mes","查询成功");
                    map.put("status","1");
                    Cookie name = new Cookie("username", username);
                    Cookie type1 = new Cookie("type", type);
                    name.setMaxAge(60*60*24*7);
                    type1.setMaxAge(60*60*24*7);
                    resp.addCookie(type1);
                    resp.addCookie(name);
                    String s = gson.toJson(map);
                    resp.getWriter().write(s);
                }else if(b == -1) {
                    map.put("mes","用户名错误");
                    map.put("status","-1");
                    String s = gson.toJson(map);
                    resp.getWriter().write(s);
                }else if( b== -2){
                    map.put("mes","密码错误");
                    map.put("status","-2");
                    String s = gson.toJson(map);
                    resp.getWriter().write(s);
                }else if(b==-3){
                    map.put("mes","账号错误");
                    map.put("status","-3");
                    String s = gson.toJson(map);
                    resp.getWriter().write(s);
                }
            }
        }else{
            Teacher teacher = new Teacher();
            com.jian.pojo.Teacher teacher1 = new com.jian.pojo.Teacher(username, password, account);
            System.out.println("是老师");
            int b = teacher.queryLogin(teacher1);
            if(b == 1){
                map.put("mes","查询成功");
                map.put("status","1");
                Cookie name = new Cookie("username", username);
                Cookie type1 = new Cookie("type", type);
                name.setMaxAge(60*60*24*7);
                type1.setMaxAge(60*60*24*7);
                resp.addCookie(type1);
                resp.addCookie(name);
                String s = gson.toJson(map);
                resp.getWriter().write(s);
            }else if(b == -1) {
                map.put("mes","用户名错误");
                map.put("status","-1");
                String s = gson.toJson(map);
                resp.getWriter().write(s);
            }else if( b== -2){
                map.put("mes","密码错误");
                map.put("status","-2");
                String s = gson.toJson(map);
                resp.getWriter().write(s);
            }else if(b==-3){
                map.put("mes","账号错误");
                map.put("status","-3");
                String s = gson.toJson(map);
                resp.getWriter().write(s);
            }
        }


    }
}
